package com.tang.kite.benchmark.mybatis

import org.apache.ibatis.mapping.Environment
import org.apache.ibatis.session.Configuration
import org.apache.ibatis.session.SqlSessionFactory
import org.apache.ibatis.session.SqlSessionFactoryBuilder
import org.apache.ibatis.transaction.TransactionFactory
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory
import javax.sql.DataSource

/**
 * @author Tang
 */
class MyBatisTester(private val dataSource: DataSource) {

    private val sqlSessionFactory: SqlSessionFactory by lazy {
        val transactionFactory: TransactionFactory = JdbcTransactionFactory()
        val configuration = Configuration(Environment("development", transactionFactory, dataSource))
        configuration.addMapper(MyBatisAccountMapper::class.java)
        SqlSessionFactoryBuilder().build(configuration)
    }

    fun select() {
        val session = sqlSessionFactory.openSession()
        val mapper = session.getMapper(MyBatisAccountMapper::class.java)
        mapper.select()
        mapper.select()
        session.close()
    }

}